package com.slm.krstartimes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.slm.krstartimes.R;
import com.slm.krstartimes.utils.Comment;
import com.slm.krstartimes.utils.MyImageLoader;
import com.slm.krstartimes.utils.TimeUtil;

import java.util.List;

/**
 * Created by dllo on 15/7/30.
 */
public class CommentAdapter extends BaseAdapter {
    private Context context;
    private List<Comment> data;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;

    public CommentAdapter(Context context, List<Comment> data) {
        this.context = context;
        this.data = data;
        requestQueue = Volley.newRequestQueue(context.getApplicationContext());
        imageLoader = new ImageLoader(requestQueue, new MyImageLoader());
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        }
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        if (data != null && data.size() > 0) {
            return data.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.comment_item, null);
            viewHolder = new ViewHolder();
            viewHolder.userPic = (ImageView) view.findViewById(R.id.userPic);
            viewHolder.userName = (TextView) view.findViewById(R.id.userName);
            viewHolder.deviceType = (TextView) view.findViewById(R.id.deviceType);
            viewHolder.floorNumber = (TextView) view.findViewById(R.id.floorNumber);
            viewHolder.location = (TextView) view.findViewById(R.id.location);
            viewHolder.comment_text = (TextView) view.findViewById(R.id.comment_text);
            viewHolder.support = (TextView) view.findViewById(R.id.support);
            viewHolder.oppose = (TextView) view.findViewById(R.id.oppose);
            viewHolder.time = (TextView) view.findViewById(R.id.time);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (data != null && data.size() > 0) {
            Comment comments = data.get(i);
            viewHolder.userName.setText(comments.getM().getN() + "");
            viewHolder.deviceType.setText(comments.getM().getTa() + "");
            viewHolder.floorNumber.setText(comments.getF() + "");
            viewHolder.location.setText(comments.getM().getY() + "");

            String time = comments.getM().getT();
            time = time.substring(6, 18);
            long temp = Long.parseLong(time);
            time = TimeUtil.getFormatTimeFromTimestamp(temp, "  MM-dd HH:MM:SS");

            viewHolder.time.setText(time + "");
            viewHolder.comment_text.setText(comments.getM().getC() + "");
            viewHolder.support.setText(comments.getM().getS() + "");
            viewHolder.oppose.setText(comments.getM().getA() + "");
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(viewHolder.userPic,
                    R.mipmap.appic, R.mipmap.appic);// 参数说明:图片地址,默认图片,加载失败显示的图片
            String u = comments.getM().getUi();
            if (u.length() == 6) {
                String a1 = u.substring(0, 2);
                String a2 = u.substring(2, 4);
                String a3 = u.substring(4, 6);
                String url = " http://i.ruanmei.com/images/upload/avatars/000/" + a1 + "/" + a2 + "/" + a3 + "_avatar.jpg ";
                imageLoader.get(url, imageListener);
            } else if (u.length() == 7) {
                String b1 = u.substring(0, 1);
                String b2 = u.substring(1, 3);
                String b3 = u.substring(3, 5);
                String b4 = u.substring(5, 7);
                String url1 = "http://i.ruanmei.com/images/upload/avatars/00" + b1 + "/" + b2 + "/" + b3 + "/" + b4 + "_avatar.jpg ";
                imageLoader.get(url1, imageListener);
            } else if (u.length() == 8) {
                String d1 = u.substring(0, 2);
                String d2 = u.substring(2, 4);
                String d3 = u.substring(4, 6);
                String d4 = u.substring(6, 8);
                String url2 = "http://i.ruanmei.com/images/upload/avatars/0" + d1 + "/" + d2 + "/" + d3 + "/" + d4 + "_avatar.jpg ";
                imageLoader.get(url2, imageListener);
            } else {
                viewHolder.userPic.setImageResource(R.mipmap.appic);
            }
        }
        return view;
    }

    public class ViewHolder {
        ImageView userPic;
        TextView userName, deviceType, floorNumber, location, time, comment_text, support, oppose;
    }
}
